package city.spring.modules.bed.service.impl;

import city.spring.modules.bed.dto.BedBuildingDTO;
import city.spring.modules.bed.entity.BedBuildingEntity;
import city.spring.modules.bed.entity.BedEntity;
import city.spring.modules.bed.entity.BedFloorEntity;
import city.spring.modules.bed.entity.BedRoomEntity;
import city.spring.modules.bed.repository.BedBuildingRepository;
import city.spring.modules.bed.service.BedBuildingService;
import city.spring.modules.bed.service.BedFloorService;
import city.spring.modules.bed.service.BedRoomService;
import city.spring.modules.bed.service.BedService;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Service：楼栋信息
 *
 * @author 侯坤林
 * @date 2020-04-26 12:01:12
 */
@CacheConfig(cacheNames = {BedBuildingServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class BedBuildingServiceImpl extends ServiceImpl<BedBuildingRepository, BedBuildingEntity> implements BedBuildingService {
    private final BedFloorService bedFloorService;
    private final BedRoomService bedRoomService;
    private final BedService bedService;

    public BedBuildingServiceImpl(BedFloorService bedFloorService, BedRoomService bedRoomService, BedService bedService) {
        this.bedFloorService = bedFloorService;
        this.bedRoomService = bedRoomService;
        this.bedService = bedService;
    }

    @Async
    @Override
    public void saveBedBuilding(BedBuildingDTO entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
        Integer floors = entity.getFloors();
        Integer rooms = entity.getRooms();
        Integer beds = entity.getBeds();
        if (floors == null || floors < 0) {
            return;
        }
        if (rooms == null || rooms < 0) {
            return;
        }
        if (beds == null || beds < 0) {
            return;
        }
        // assert floors != null : "楼层数不能为空";
        // assert rooms != null : "房间数不能为空";
        // assert beds != null : "床位数不能为空";
        //
        // assert floors >= 0 : "楼层数不能为负数";
        // assert rooms >= 0 : "房间数不能为负数";
        // assert beds >= 0 : "床位数不能为负数";
        // 先保存所有楼层信息
        List<BedFloorEntity> floorEntities = new ArrayList<>();
        for (int floorIndex = 0; floorIndex < floors; floorIndex++) {
            BedFloorEntity floorEntity = new BedFloorEntity();
            floorEntity.setId(IdWorker.getIdStr());
            floorEntity.setBedBuildingId(entity.getId());
            floorEntity.setCode(String.valueOf(floorIndex + 1));
            floorEntity.setName(String.format("%d层", floorIndex + 1));
            floorEntities.add(floorEntity);
        }

        // 然后保存所有楼层的房间信息
        List<BedRoomEntity> roomEntities = new ArrayList<>();
        for (int floorIndex = 0; floorIndex < floorEntities.size(); floorIndex++) {
            BedFloorEntity floorEntity = floorEntities.get(floorIndex);

            for (int roomIndex = 0; roomIndex < rooms; roomIndex++) {
                BedRoomEntity roomEntity = new BedRoomEntity();
                roomEntity.setId(IdWorker.getIdStr());
                roomEntity.setBedFloorId(floorEntity.getId());
                roomEntity.setRoomTypeId(entity.getRoomsType());
                roomEntity.setCode(String.format("%d%02d", floorIndex + 1, roomIndex + 1));
                roomEntity.setName(String.format("%d%02d房间", floorIndex + 1, roomIndex + 1));
                roomEntity.setDirection(entity.getRoomsDirection());
                roomEntities.add(roomEntity);
            }
        }

        // 最后保存所有房间的床位信息
        List<BedEntity> bedEntities = new ArrayList<>();
        for (BedRoomEntity roomEntity : roomEntities) {
            for (int bedIndex = 0; bedIndex < beds; bedIndex++) {
                BedEntity bedEntity = new BedEntity();
                bedEntity.setId(IdWorker.getIdStr());
                bedEntity.setBedRoomId(roomEntity.getId());
                bedEntity.setCode(String.valueOf(bedIndex + 1));
                bedEntity.setName(String.format("%d号床", bedIndex + 1));
                bedEntity.setDirection(entity.getBedsDirection());
                bedEntities.add(bedEntity);
            }
        }
        bedFloorService.saveBatch(floorEntities);
        bedRoomService.saveBatch(roomEntities);
        bedService.saveBatch(bedEntities);
    }

    @Override
    public void updateBedBuilding(BedBuildingEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteBedBuilding(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteBedBuilding(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }
}